
package ling.archives.controller;

import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import ling.archives.domain.DocumentClass;
//import ling.archives.service.DocumentClassService;
import ling.archives.common.*;
import ling.archives.common.domain.*;

/** 文件分类 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-4-2
 */
@Api(tags = "文件分类 功能接口")
@RestController
@RequestMapping("/archives/DocumentClass")
public class DocumentClassController extends EQueryController<DocumentClass>{



    @ApiOperation("详情")
    @PreAuthorize("@ar.any(@ar.documentClass.query)")
    @GetMapping(value = "/{id}")
    public Result<DocumentClass> getInfo(@PathVariable("id") Long id)
    {
        var re = queryHelper.entityQuery(DocumentClass.class)
                .whereById(id).asTreeCTE().toTreeList();
        if(re.isEmpty())
            return Result.ok();
        return Result.ok(re.get(0)); //super.getInfo(id);
    }

    @ApiOperation(value = "树形查询分类",notes = "返回指定分类，包含子节点,支持排序")
    @PreAuthorize("@ar.any(@ar.documentClass.list)")
    @GetMapping("tree")
    public Result<List<DocumentClass>> tree(DocumentClass dto,PageSort pageRequest) {
        dto.setType(0);
        var list = queryHelper.entityQuery(DocumentClass.class)
                .whereObject(dto).asTreeCTE().orderByObject(pageRequest).toTreeList();
        return Result.ok(list);
    }

    @ApiOperation("分页列表")
    @PreAuthorize("@ar.any(@ar.documentClass.list)")
    @GetMapping("list")
    public ResultRows<DocumentClass> list(DocumentClass dto, PageSort pageRequest) {
        return queryPage(dto,pageRequest);
    }

    @ApiOperation("新增")
    @PreAuthorize("@ar.any(@ar.documentClass.add)")
    @Log(title = "文件分类", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Result add(@Validated @RequestBody DocumentClass entity) {
        if (entity.getType() != 0)
            return Result.bad("只能新建分类");
        var exist = queryHelper.entityQuery(DocumentClass.class).where(d -> {
            d.parentId().eq(entity.getParentId()!=null,entity.getParentId());
            d.title().eq(entity.getTitle());
        }).any();
        if (exist)
            return Result.bad("名称重复");
        if(entity.getParentId()!=null)
        {
            var parent=queryHelper.entityQuery(DocumentClass.class).whereById(entity.getParentId()).firstOrNull();
            entity.setPath(parent.getPath()+"/"+parent.getParentId());
        }
        return super.add(entity);
    }
    @ApiOperation("删除")
    @PreAuthorize("@ar.any(@ar.documentClass.remove)")
    @Log(title = "文件分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation(value = "编辑",notes = "当前只运行改名称")
    @PreAuthorize("@ar.any(@ar.documentClass.edit)")
    @Log(title = "文件分类", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  Result edit(@Validated @RequestBody DocumentClass entity){
        var old=queryHelper.queryable(DocumentClass.class).whereById(entity.getId()).firstOrNull();
        old.setTitle(entity.getTitle());
        return super.edit(entity);
    }
//    @ApiOperation("导出")
//    @PreAuthorize("@ar.any(@ar.documentClass.export)")
//    @Log(title = "文件分类", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(DocumentClass entity, PageDomain pageRequest,HttpServletResponse response)
//    {
//        super.export(entity,pageRequest,null,response);
//    }
}